﻿using System.Management;

namespace TroubleshootingofBeanBagAssistant
{
	public class AntivirusInfo
	{
		public string DisplayName { get; set; }
		public string ProductState { get; set; }
		public string PathToExe { get; set; }
	}

	public static class AntivirusHelper
	{
		/// <summary>
		/// 获取所有已注册的杀毒软件信息
		/// </summary>
		public static List<AntivirusInfo> GetAntivirusStatus()
		{
			var result = new List<AntivirusInfo>();
			try
			{
				using var searcher = new ManagementObjectSearcher(@"root\SecurityCenter2", "SELECT * FROM AntiVirusProduct");
				foreach (ManagementObject av in searcher.Get())
				{


					var info = new AntivirusInfo
					{
						DisplayName = av["displayName"]?.ToString(),
						ProductState = av["productState"]?.ToString(),
						// 获取产品状态（十六进制）

						//PathToExe = av["pathToExe"]?.ToString()
					};
					result.Add(info);
				}
			}
			catch (Exception ex)
			{
				// 处理异常（如无权限、WMI不可用等）
			}
			return result;
		}





		/// <summary>
		/// 判断是否有启用的杀毒软件
		/// </summary>
		public static bool HasActiveAntivirus()
		{
			foreach (var av in GetAntivirusStatus())
			{
				// productState的高字节为0x10表示已启用
				if (!string.IsNullOrEmpty(av.ProductState) && av.ProductState.Length >= 6)
				{
					int state = Convert.ToInt32(av.ProductState, 16);
					if ((state & 0x0010) == 0x0010)
						return true;
				}
			}
			return false;
		}
	}
}
